#include "../../../common/data_mysql.hpp"
#include "../../../odb/user.hxx"
#include "user-odb.hxx"
#include <mysql/mysql.h>
#include <gflags/gflags.h>

DEFINE_bool(run_mode, false, "程序的运行模式, false-调试; true-发布;");
DEFINE_string(log_file, "", "发布模式下, 用于指定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下, 用于指定日志输出等级");  //trace default 0

void insert(xchat::UserTable& user_table)
{
    auto user1 = std::make_shared<xchat::User>("uid1", "nickname1", "123456");
    user_table.insert(user1);  
    auto user2 = std::make_shared<xchat::User>("uid2", "157111111ake");
    user_table.insert(user2);  
}
void update_by_id(xchat::UserTable& user_table)
{
    auto user = user_table.select_by_id("uid1");
    if (user.get())
    {
        user->description("我是一个测试用户");
        user_table.update(user);
    }
    else
    {
        LOG_ERROR("查询user指针为空");
    }
}
void update_by_phone(xchat::UserTable& user_table)
{
    auto user = user_table.select_by_phone("157111111ake");
    if (user.get())
    {
        user->password("23232323232");
        user_table.update(user);
    }
    else
    {
        LOG_ERROR("查询user指针为空");
    }
}
void update_by_nickname(xchat::UserTable& user_table)
{
    auto user = user_table.select_by_nickname("uid2");
    if (user.get())
    {
        user->nickname("我是uid2");
        user_table.update(user);
    }
    else
    {
        LOG_ERROR("查询user指针为空");
    }
}
void select_users(xchat::UserTable& user_table, const std::vector<std::string>& id_lists)
{
    auto users = user_table.select_multi_users(id_lists);
    for (auto& user : users)
    {
        LOG_INFO("user nickname: {}", !user.nickname().null() ? user.nickname().get() : "");
    }
}

int main(int argc, char* argv[])
{
    google::ParseCommandLineFlags(&argc, &argv, true);
    
    xchat::init_logger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);

    auto db = xchat::ODBFactory::create("root", "299274142", "127.0.0.1", "xchat", "utf8", 0, 1);

    xchat::UserTable user_table(db);

    //insert(user_table);

    //update_by_id(user_table);

    //update_by_phone(user_table);

    //update_by_nickname(user_table);

    select_users(user_table, std::vector<std::string>({"uid1", "uid2"}));
    return 0;
}